package com.bs.service.impl;

import java.util.*;
import com.bs.bean.Instrument;
import com.bs.service.InstrumentService;
import com.bs.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
/*业务层：InstrumentService接口的实现类，调用InstrumentDao接口的实现类对数据进行相关业务等操作*/
public class InstrumentServiceImpl implements InstrumentService{

//	注入InstrumentDao接口的实现类对象
	@Autowired
	InstrumentMapper instrumentDao;
//	根据条件查询instrument中的数据
	public List<Instrument> findAll(Instrument query) {
		// TODO Auto-generated method stub
		return findAll(query==null?new Instrument():query,true);
	}
	
//	根据条件和模式查询instrument中的数据
	public List<Instrument> findAll(Instrument whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Instrument> list = instrumentDao.selectByExample(whereWrap);
		if (whereWrap!=null) {
			Iterator<Instrument> iterator = list.iterator();
			while (iterator.hasNext()) {
				Instrument instrument = (Instrument) iterator.next();
				if (whereWrap.getId() != null&&(!(whereWrap.getId()+"").equals(""))) {
					if (mode) {
						if (!(instrument.getId() + "").equals(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(instrument.getId() + "").contains(whereWrap.getId() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getName() != null&&(!(whereWrap.getName()+"").equals(""))) {
					if (mode) {
						if (!(instrument.getName() + "").equals(whereWrap.getName() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(instrument.getName() + "").contains(whereWrap.getName() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getRate() != null&&(!(whereWrap.getRate()+"").equals(""))) {
					if (mode) {
						if (!(instrument.getRate() + "").equals(whereWrap.getRate() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(instrument.getRate() + "").contains(whereWrap.getRate() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPrice() != null&&(!(whereWrap.getPrice()+"").equals(""))) {
					if (mode) {
						if (!(instrument.getPrice() + "").equals(whereWrap.getPrice() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(instrument.getPrice() + "").contains(whereWrap.getPrice() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
				if (whereWrap.getPic() != null&&(!(whereWrap.getPic()+"").equals(""))) {
					if (mode) {
						if (!(instrument.getPic() + "").equals(whereWrap.getPic() + "")) {
							iterator.remove();
							continue;
						}
					} else {
						if (!(instrument.getPic() + "").contains(whereWrap.getPic() + "")) {
							iterator.remove();
							continue;
						}
					}
				}
			} 
		}
		return list;
	}
	
//	查询instrument中的数据
	public List<Instrument> findAll() {
		// TODO Auto-generated method stub
		return findAll(new Instrument());
	}
	
//	根据主键查询instrument中的一条数据
	public Instrument findById(Integer id) {
		// TODO Auto-generated method stub
		return instrumentDao.selectByPrimaryKey(id);
	}

//	选择性添加instrument表中的instrument数据
	public int insert(Instrument instrument) {
		// TODO Auto-generated method stub
		return instrumentDao.insertSelective(instrument);
	}

//	根据主键更新instrument表中的一条记录
	public int updateById(Instrument instrument) {
		// TODO Auto-generated method stub
		return instrumentDao.updateByPrimaryKey(instrument);
	}

//	根据主键选择性更新instrument表中的instrument数据
	public int updateByIdSelective(Instrument instrument) {
		// TODO Auto-generated method stub
		Instrument dbInstrument = findById(instrument.getId());
		if (instrument.getId()==null) {
			instrument.setId(dbInstrument.getId());
		}
		if (instrument.getName()==null) {
			instrument.setName(dbInstrument.getName());
		}
		if (instrument.getRate()==null) {
			instrument.setRate(dbInstrument.getRate());
		}
		if (instrument.getPrice()==null) {
			instrument.setPrice(dbInstrument.getPrice());
		}
		if (instrument.getPic()==null) {
			instrument.setPic(dbInstrument.getPic());
		}
		return updateById(instrument);
	}

//	根据条件删除instrument表中的数据
	public int delete(Instrument whereWrap) {
		// TODO Auto-generated method stub
		return delete(whereWrap,true);
	}

//	根据条件和模式删除instrument表中的数据
	public int delete(Instrument whereWrap, boolean mode) {
		// TODO Auto-generated method stub
		List<Instrument> list = findAll(whereWrap, mode);
		for (Instrument instrument : list) {
			deleteById(instrument.getId());
		}
		return list.size();
	}
//	根据主键删除instrument表中的一条记录
	public int deleteById(Integer id) {
		// TODO Auto-generated method stub
		return instrumentDao.deleteByPrimaryKey(id);
	}

//	根据主键集合批量删除instrument表中的记录
	public int deleteByIds(List<Integer> list){
		for (Integer id : list) {
			deleteById(id);
		}
		return list.size();
	}
}
